package org.apache.commons.lang.builder;

import java.util.Collection;

@SuppressWarnings({ "rawtypes" })
public class HashCodeBuilder {

	/**
	 * <p>
	 * Appends the fields and values defined by the given object of the given <code>Class</code>.
	 * </p>
	 * @param object the object to append details of
	 * @param clazz the class to append details of
	 * @param builder the builder to append to
	 * @param useTransients whether to use transient fields
	 * @param excludeFields Collection of String field names to exclude from use in calculation of hash code
	 */
	private static void reflectionAppend(Object object, Class clazz, HashCodeBuilder builder, boolean useTransients, String[] excludeFields) {
		throw new UnsupportedOperationException("reflectionAppend()");
	}

	/**
	 * <p>
	 * This method uses reflection to build a valid hash code.
	 * </p>
	 * <p>
	 * It uses <code>AccessibleObject.setAccessible</code> to gain access to private fields. This means that it will
	 * throw a security exception if run under a security manager, if the permissions are not set up correctly. It is
	 * also not as efficient as testing explicitly.
	 * </p>
	 * <p>
	 * Transient members will be not be used, as they are likely derived fields, and not part of the value of the
	 * <code>Object</code>.
	 * </p>
	 * <p>
	 * Static fields will not be tested. Superclass fields will be included.
	 * </p>
	 * <p>
	 * Two randomly chosen, non-zero, odd numbers must be passed in. Ideally these should be different for each class,
	 * however this is not vital. Prime numbers are preferred, especially for the multiplier.
	 * </p>
	 * @param initialNonZeroOddNumber a non-zero, odd number used as the initial value
	 * @param multiplierNonZeroOddNumber a non-zero, odd number used as the multiplier
	 * @param object the Object to create a <code>hashCode</code> for
	 * @return int hash code
	 * @throws IllegalArgumentException if the Object is <code>null</code>
	 * @throws IllegalArgumentException if the number is zero or even
	 */
	public static int reflectionHashCode(int initialNonZeroOddNumber, int multiplierNonZeroOddNumber, Object object) {
		return reflectionHashCode(initialNonZeroOddNumber, multiplierNonZeroOddNumber, object, false, null, null);
	}

	/**
	 * <p>
	 * This method uses reflection to build a valid hash code.
	 * </p>
	 * <p>
	 * It uses <code>AccessibleObject.setAccessible</code> to gain access to private fields. This means that it will
	 * throw a security exception if run under a security manager, if the permissions are not set up correctly. It is
	 * also not as efficient as testing explicitly.
	 * </p>
	 * <p>
	 * If the TestTransients parameter is set to <code>true</code>, transient members will be tested, otherwise they are
	 * ignored, as they are likely derived fields, and not part of the value of the <code>Object</code>.
	 * </p>
	 * <p>
	 * Static fields will not be tested. Superclass fields will be included.
	 * </p>
	 * <p>
	 * Two randomly chosen, non-zero, odd numbers must be passed in. Ideally these should be different for each class,
	 * however this is not vital. Prime numbers are preferred, especially for the multiplier.
	 * </p>
	 * @param initialNonZeroOddNumber a non-zero, odd number used as the initial value
	 * @param multiplierNonZeroOddNumber a non-zero, odd number used as the multiplier
	 * @param object the Object to create a <code>hashCode</code> for
	 * @param testTransients whether to include transient fields
	 * @return int hash code
	 * @throws IllegalArgumentException if the Object is <code>null</code>
	 * @throws IllegalArgumentException if the number is zero or even
	 */
	public static int reflectionHashCode(int initialNonZeroOddNumber, int multiplierNonZeroOddNumber, Object object, boolean testTransients) {
		return reflectionHashCode(initialNonZeroOddNumber, multiplierNonZeroOddNumber, object, testTransients, null, null);
	}

	/**
	 * Calls {@link #reflectionHashCode(int, int, Object, boolean, Class, String[])} with excludeFields set to
	 * <code>null</code>.
	 * @param initialNonZeroOddNumber a non-zero, odd number used as the initial value
	 * @param multiplierNonZeroOddNumber a non-zero, odd number used as the multiplier
	 * @param object the Object to create a <code>hashCode</code> for
	 * @param testTransients whether to include transient fields
	 * @param reflectUpToClass the superclass to reflect up to (inclusive), may be <code>null</code>
	 * @return int hash code
	 */
	public static int reflectionHashCode(int initialNonZeroOddNumber, int multiplierNonZeroOddNumber, Object object, boolean testTransients, Class reflectUpToClass) {
		return reflectionHashCode(initialNonZeroOddNumber, multiplierNonZeroOddNumber, object, testTransients, reflectUpToClass, null);
	}

	/**
	 * <p>
	 * This method uses reflection to build a valid hash code.
	 * </p>
	 * <p>
	 * It uses <code>AccessibleObject.setAccessible</code> to gain access to private fields. This means that it will
	 * throw a security exception if run under a security manager, if the permissions are not set up correctly. It is
	 * also not as efficient as testing explicitly.
	 * </p>
	 * <p>
	 * If the TestTransients parameter is set to <code>true</code>, transient members will be tested, otherwise they are
	 * ignored, as they are likely derived fields, and not part of the value of the <code>Object</code>.
	 * </p>
	 * <p>
	 * Static fields will not be included. Superclass fields will be included up to and including the specified
	 * superclass. A null superclass is treated as java.lang.Object.
	 * </p>
	 * <p>
	 * Two randomly chosen, non-zero, odd numbers must be passed in. Ideally these should be different for each class,
	 * however this is not vital. Prime numbers are preferred, especially for the multiplier.
	 * </p>
	 * @param initialNonZeroOddNumber a non-zero, odd number used as the initial value
	 * @param multiplierNonZeroOddNumber a non-zero, odd number used as the multiplier
	 * @param object the Object to create a <code>hashCode</code> for
	 * @param testTransients whether to include transient fields
	 * @param reflectUpToClass the superclass to reflect up to (inclusive), may be <code>null</code>
	 * @param excludeFields array of field names to exclude from use in calculation of hash code
	 * @return int hash code
	 * @throws IllegalArgumentException if the Object is <code>null</code>
	 * @throws IllegalArgumentException if the number is zero or even
	 * @since 2.0
	 */
	public static int reflectionHashCode(int initialNonZeroOddNumber, int multiplierNonZeroOddNumber, Object object, boolean testTransients, Class reflectUpToClass, String[] excludeFields) {

		if (object == null) {
			throw new IllegalArgumentException("The object to build a hash code for must not be null");
		}
		HashCodeBuilder builder = new HashCodeBuilder(initialNonZeroOddNumber, multiplierNonZeroOddNumber);
		Class clazz = object.getClass();
		reflectionAppend(object, clazz, builder, testTransients, excludeFields);
		while (clazz.getSuperclass() != null && clazz != reflectUpToClass) {
			clazz = clazz.getSuperclass();
			reflectionAppend(object, clazz, builder, testTransients, excludeFields);
		}
		return builder.toHashCode();
	}

	/**
	 * <p>
	 * This method uses reflection to build a valid hash code.
	 * </p>
	 * <p>
	 * This constructor uses two hard coded choices for the constants needed to build a hash code.
	 * </p>
	 * <p>
	 * It uses <code>AccessibleObject.setAccessible</code> to gain access to private fields. This means that it will
	 * throw a security exception if run under a security manager, if the permissions are not set up correctly. It is
	 * also not as efficient as testing explicitly.
	 * </p>
	 * <p>
	 * Transient members will be not be used, as they are likely derived fields, and not part of the value of the
	 * <code>Object</code>.
	 * </p>
	 * <p>
	 * Static fields will not be tested. Superclass fields will be included.
	 * </p>
	 * @param object the Object to create a <code>hashCode</code> for
	 * @return int hash code
	 * @throws IllegalArgumentException if the object is <code>null</code>
	 */
	public static int reflectionHashCode(Object object) {
		return reflectionHashCode(17, 37, object, false, null, null);
	}

	/**
	 * <p>
	 * This method uses reflection to build a valid hash code.
	 * </p>
	 * <p>
	 * This constructor uses two hard coded choices for the constants needed to build a hash code.
	 * </p>
	 * <p>
	 * It uses <code>AccessibleObject.setAccessible</code> to gain access to private fields. This means that it will
	 * throw a security exception if run under a security manager, if the permissions are not set up correctly. It is
	 * also not as efficient as testing explicitly.
	 * </p>
	 * <P>
	 * If the TestTransients parameter is set to <code>true</code>, transient members will be tested, otherwise they are
	 * ignored, as they are likely derived fields, and not part of the value of the <code>Object</code>.
	 * </p>
	 * <p>
	 * Static fields will not be tested. Superclass fields will be included.
	 * </p>
	 * @param object the Object to create a <code>hashCode</code> for
	 * @param testTransients whether to include transient fields
	 * @return int hash code
	 * @throws IllegalArgumentException if the object is <code>null</code>
	 */
	public static int reflectionHashCode(Object object, boolean testTransients) {
		return reflectionHashCode(17, 37, object, testTransients, null, null);
	}

	/**
	 * <p>
	 * This method uses reflection to build a valid hash code.
	 * </p>
	 * <p>
	 * This constructor uses two hard coded choices for the constants needed to build a hash code.
	 * </p>
	 * <p>
	 * It uses <code>AccessibleObject.setAccessible</code> to gain access to private fields. This means that it will
	 * throw a security exception if run under a security manager, if the permissions are not set up correctly. It is
	 * also not as efficient as testing explicitly.
	 * </p>
	 * <p>
	 * Transient members will be not be used, as they are likely derived fields, and not part of the value of the
	 * <code>Object</code>.
	 * </p>
	 * <p>
	 * Static fields will not be tested. Superclass fields will be included.
	 * </p>
	 * @param object the Object to create a <code>hashCode</code> for
	 * @param excludeFields Collection of String field names to exclude from use in calculation of hash code
	 * @return int hash code
	 * @throws IllegalArgumentException if the object is <code>null</code>
	 */
	public static int reflectionHashCode(Object object, Collection /* String */excludeFields) {
		throw new UnsupportedOperationException("reflectionHashCode()");
	}

	// -------------------------------------------------------------------------

	/**
	 * <p>
	 * This method uses reflection to build a valid hash code.
	 * </p>
	 * <p>
	 * This constructor uses two hard coded choices for the constants needed to build a hash code.
	 * </p>
	 * <p>
	 * It uses <code>AccessibleObject.setAccessible</code> to gain access to private fields. This means that it will
	 * throw a security exception if run under a security manager, if the permissions are not set up correctly. It is
	 * also not as efficient as testing explicitly.
	 * </p>
	 * <p>
	 * Transient members will be not be used, as they are likely derived fields, and not part of the value of the
	 * <code>Object</code>.
	 * </p>
	 * <p>
	 * Static fields will not be tested. Superclass fields will be included.
	 * </p>
	 * @param object the Object to create a <code>hashCode</code> for
	 * @param excludeFields array of field names to exclude from use in calculation of hash code
	 * @return int hash code
	 * @throws IllegalArgumentException if the object is <code>null</code>
	 */
	public static int reflectionHashCode(Object object, String[] excludeFields) {
		return reflectionHashCode(17, 37, object, false, null, excludeFields);
	}

	/**
	 * <p>
	 * Registers the given object. Used by the reflection methods to avoid infinite loops.
	 * </p>
	 * @param value The object to register.
	 */
	static void register(Object value) {
	}

	/**
	 * <p>
	 * Unregisters the given object.
	 * </p>
	 * <p>
	 * Used by the reflection methods to avoid infinite loops.
	 * @param value The object to unregister.
	 * @since 2.3
	 */
	static void unregister(Object value) {
	}

	/**
	 * Constant to use in building the hashCode.
	 */
	private final int iConstant;

	/**
	 * Running total of the hashCode.
	 */
	private int iTotal = 0;

	/**
	 * <p>
	 * Uses two hard coded choices for the constants needed to build a <code>hashCode</code>.
	 * </p>
	 */
	public HashCodeBuilder() {
		iConstant = 37;
		iTotal = 17;
	}

	/**
	 * <p>
	 * Two randomly chosen, non-zero, odd numbers must be passed in. Ideally these should be different for each class,
	 * however this is not vital.
	 * </p>
	 * <p>
	 * Prime numbers are preferred, especially for the multiplier.
	 * </p>
	 * @param initialNonZeroOddNumber a non-zero, odd number used as the initial value
	 * @param multiplierNonZeroOddNumber a non-zero, odd number used as the multiplier
	 * @throws IllegalArgumentException if the number is zero or even
	 */
	public HashCodeBuilder(int initialNonZeroOddNumber, int multiplierNonZeroOddNumber) {
		if (initialNonZeroOddNumber == 0) {
			throw new IllegalArgumentException("HashCodeBuilder requires a non zero initial value");
		}
		if (initialNonZeroOddNumber % 2 == 0) {
			throw new IllegalArgumentException("HashCodeBuilder requires an odd initial value");
		}
		if (multiplierNonZeroOddNumber == 0) {
			throw new IllegalArgumentException("HashCodeBuilder requires a non zero multiplier");
		}
		if (multiplierNonZeroOddNumber % 2 == 0) {
			throw new IllegalArgumentException("HashCodeBuilder requires an odd multiplier");
		}
		iConstant = multiplierNonZeroOddNumber;
		iTotal = initialNonZeroOddNumber;
	}

	/**
	 * <p>
	 * Append a <code>hashCode</code> for a <code>boolean</code>.
	 * </p>
	 * <p>
	 * This adds <code>iConstant * 1</code> to the <code>hashCode</code> and not a <code>1231</code> or
	 * <code>1237</code> as done in java.lang.Boolean. This is in accordance with the <quote>Effective Java</quote>
	 * design.
	 * </p>
	 * @param value the boolean to add to the <code>hashCode</code>
	 * @return this
	 */
	public HashCodeBuilder append(boolean value) {
		iTotal = iTotal * iConstant + (value ? 0 : 1);
		return this;
	}

	/**
	 * <p>
	 * Append a <code>hashCode</code> for a <code>boolean</code> array.
	 * </p>
	 * @param array the array to add to the <code>hashCode</code>
	 * @return this
	 */
	public HashCodeBuilder append(boolean[] array) {
		if (array == null) {
			iTotal = iTotal * iConstant;
		} else {
			for (int i = 0; i < array.length; i++) {
				append(array[i]);
			}
		}
		return this;
	}

	// -------------------------------------------------------------------------

	/**
	 * <p>
	 * Append a <code>hashCode</code> for a <code>byte</code>.
	 * </p>
	 * @param value the byte to add to the <code>hashCode</code>
	 * @return this
	 */
	public HashCodeBuilder append(byte value) {
		iTotal = iTotal * iConstant + value;
		return this;
	}

	// -------------------------------------------------------------------------

	/**
	 * <p>
	 * Append a <code>hashCode</code> for a <code>byte</code> array.
	 * </p>
	 * @param array the array to add to the <code>hashCode</code>
	 * @return this
	 */
	public HashCodeBuilder append(byte[] array) {
		if (array == null) {
			iTotal = iTotal * iConstant;
		} else {
			for (int i = 0; i < array.length; i++) {
				append(array[i]);
			}
		}
		return this;
	}

	/**
	 * <p>
	 * Append a <code>hashCode</code> for a <code>char</code>.
	 * </p>
	 * @param value the char to add to the <code>hashCode</code>
	 * @return this
	 */
	public HashCodeBuilder append(char value) {
		iTotal = iTotal * iConstant + value;
		return this;
	}

	/**
	 * <p>
	 * Append a <code>hashCode</code> for a <code>char</code> array.
	 * </p>
	 * @param array the array to add to the <code>hashCode</code>
	 * @return this
	 */
	public HashCodeBuilder append(char[] array) {
		if (array == null) {
			iTotal = iTotal * iConstant;
		} else {
			for (int i = 0; i < array.length; i++) {
				append(array[i]);
			}
		}
		return this;
	}

	/**
	 * <p>
	 * Append a <code>hashCode</code> for a <code>double</code>.
	 * </p>
	 * @param value the double to add to the <code>hashCode</code>
	 * @return this
	 */
	public HashCodeBuilder append(double value) {
		return append((long) value);
	}

	/**
	 * <p>
	 * Append a <code>hashCode</code> for a <code>double</code> array.
	 * </p>
	 * @param array the array to add to the <code>hashCode</code>
	 * @return this
	 */
	public HashCodeBuilder append(double[] array) {
		if (array == null) {
			iTotal = iTotal * iConstant;
		} else {
			for (int i = 0; i < array.length; i++) {
				append(array[i]);
			}
		}
		return this;
	}

	/**
	 * <p>
	 * Append a <code>hashCode</code> for a <code>float</code>.
	 * </p>
	 * @param value the float to add to the <code>hashCode</code>
	 * @return this
	 */
	public HashCodeBuilder append(float value) {
		iTotal = iTotal * iConstant + (int) value;
		return this;
	}

	/**
	 * <p>
	 * Append a <code>hashCode</code> for a <code>float</code> array.
	 * </p>
	 * @param array the array to add to the <code>hashCode</code>
	 * @return this
	 */
	public HashCodeBuilder append(float[] array) {
		if (array == null) {
			iTotal = iTotal * iConstant;
		} else {
			for (int i = 0; i < array.length; i++) {
				append(array[i]);
			}
		}
		return this;
	}

	/**
	 * <p>
	 * Append a <code>hashCode</code> for an <code>int</code>.
	 * </p>
	 * @param value the int to add to the <code>hashCode</code>
	 * @return this
	 */
	public HashCodeBuilder append(int value) {
		iTotal = iTotal * iConstant + value;
		return this;
	}

	/**
	 * <p>
	 * Append a <code>hashCode</code> for an <code>int</code> array.
	 * </p>
	 * @param array the array to add to the <code>hashCode</code>
	 * @return this
	 */
	public HashCodeBuilder append(int[] array) {
		if (array == null) {
			iTotal = iTotal * iConstant;
		} else {
			for (int i = 0; i < array.length; i++) {
				append(array[i]);
			}
		}
		return this;
	}

	/**
	 * <p>
	 * Append a <code>hashCode</code> for a <code>long</code>.
	 * </p>
	 * @param value the long to add to the <code>hashCode</code>
	 * @return this
	 */
	// NOTE: This method uses >> and not >>> as Effective Java and
	// Long.hashCode do. Ideally we should switch to >>> at
	// some stage. There are backwards compat issues, so
	// that will have to wait for the time being. cf LANG-342.
	public HashCodeBuilder append(long value) {
		iTotal = iTotal * iConstant + ((int) (value ^ (value >> 32)));
		return this;
	}

	/**
	 * <p>
	 * Append a <code>hashCode</code> for a <code>long</code> array.
	 * </p>
	 * @param array the array to add to the <code>hashCode</code>
	 * @return this
	 */
	public HashCodeBuilder append(long[] array) {
		if (array == null) {
			iTotal = iTotal * iConstant;
		} else {
			for (int i = 0; i < array.length; i++) {
				append(array[i]);
			}
		}
		return this;
	}

	/**
	 * <p>
	 * Append a <code>hashCode</code> for an <code>Object</code>.
	 * </p>
	 * @param object the Object to add to the <code>hashCode</code>
	 * @return this
	 */
	public HashCodeBuilder append(Object object) {
		if (object == null) {
			iTotal = iTotal * iConstant;

		} else {
			if (object.getClass().isArray()) {
				// 'Switch' on type of array, to dispatch to the correct handler
				// This handles multi dimensional arrays
				if (object instanceof long[]) {
					append((long[]) object);
				} else if (object instanceof int[]) {
					append((int[]) object);
				} else if (object instanceof short[]) {
					append((short[]) object);
				} else if (object instanceof char[]) {
					append((char[]) object);
				} else if (object instanceof byte[]) {
					append((byte[]) object);
				} else if (object instanceof double[]) {
					append((double[]) object);
				} else if (object instanceof float[]) {
					append((float[]) object);
				} else if (object instanceof boolean[]) {
					append((boolean[]) object);
				} else {
					// Not an array of primitives
					append((Object[]) object);
				}
			} else {
				iTotal = iTotal * iConstant + object.hashCode();
			}
		}
		return this;
	}

	/**
	 * <p>
	 * Append a <code>hashCode</code> for an <code>Object</code> array.
	 * </p>
	 * @param array the array to add to the <code>hashCode</code>
	 * @return this
	 */
	public HashCodeBuilder append(Object[] array) {
		if (array == null) {
			iTotal = iTotal * iConstant;
		} else {
			for (int i = 0; i < array.length; i++) {
				append(array[i]);
			}
		}
		return this;
	}

	/**
	 * <p>
	 * Append a <code>hashCode</code> for a <code>short</code>.
	 * </p>
	 * @param value the short to add to the <code>hashCode</code>
	 * @return this
	 */
	public HashCodeBuilder append(short value) {
		iTotal = iTotal * iConstant + value;
		return this;
	}

	/**
	 * <p>
	 * Append a <code>hashCode</code> for a <code>short</code> array.
	 * </p>
	 * @param array the array to add to the <code>hashCode</code>
	 * @return this
	 */
	public HashCodeBuilder append(short[] array) {
		if (array == null) {
			iTotal = iTotal * iConstant;
		} else {
			for (int i = 0; i < array.length; i++) {
				append(array[i]);
			}
		}
		return this;
	}

	/**
	 * <p>
	 * Adds the result of super.hashCode() to this builder.
	 * </p>
	 * @param superHashCode the result of calling <code>super.hashCode()</code>
	 * @return this HashCodeBuilder, used to chain calls.
	 * @since 2.0
	 */
	public HashCodeBuilder appendSuper(int superHashCode) {
		iTotal = iTotal * iConstant + superHashCode;
		return this;
	}

	/**
	 * <p>
	 * Return the computed <code>hashCode</code>.
	 * </p>
	 * @return <code>hashCode</code> based on the fields appended
	 */
	public int toHashCode() {
		return iTotal;
	}

	/**
	 * <p>
	 * The computed <code>hashCode</code> from toHashCode() is returned due to the likelyhood of bugs in mis-calling
	 * toHashCode() and the unlikelyness of it mattering what the hashCode for HashCodeBuilder itself is.
	 * @return <code>hashCode</code> based on the fields appended
	 * @since 2.5
	 */
	@Override
	public int hashCode() {
		return toHashCode();
	}

}
